package com.bilibili.server.controller;

import com.bilibili.model.vo.PageResult;
import com.bilibili.model.vo.UserInfoVo;
import com.bilibili.model.vo.UserInfoVoLike;
import com.bilibili.server.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @author HuangRongFu
 * 2021/11/17 星期三4:35 下午
 */

@RestController
@RequestMapping("/users")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    /**
     *  查询用户详细信息
     *@params: userID
     *@params: huanxinID
     *@params: token
     *@Return: ResponseEntity
     */
    @GetMapping
    public ResponseEntity getUserInfo(Long userID, Long huanxinID) {

        UserInfoVo vo = userInfoService.getUserInfo(userID, huanxinID);
        return ResponseEntity.ok(vo);
    }

    /**
     * 更新用户信息
     *@params: userInfoVo
     *@Return: ResponseEntity
     */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfoVo vo){

        userInfoService.updateUserInfo(vo);

        return ResponseEntity.ok(null);
    }


    /**
     * 更换头像
     * @param headPhoto
     * @Return: ResponseEntity
     */
    @PostMapping("/header")
    public ResponseEntity updateHeader(@RequestBody MultipartFile headPhoto){

        userInfoService.updateHeader(headPhoto);

        return ResponseEntity.ok(null);
    }

    /**
     * 修改手机号-发送验证码
     * @Return: ResponseEntity
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode(){

        userInfoService.sendVerificationCode();

        return ResponseEntity.ok(null);
    }

    /**
     * 修改手机号-校验验证码
     * @Return: ResponseEntity
     */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map<String, String> paramMap){

        Map<String,Boolean> resultMap = userInfoService.checkVerificationCode(paramMap);

        return ResponseEntity.ok(resultMap);
    }

    /**
     * 修改手机号-保存
     * @Return: ResponseEntity
     */
    @PostMapping("/phone")
    public ResponseEntity savePhone(@RequestBody Map<String, String> paramMap){

       userInfoService.updatePhoneNumber(paramMap);

        return ResponseEntity.ok(null);
    }

    @GetMapping("/friends/{type}")
    public ResponseEntity findFriends(@PathVariable(value = "type") Integer type,
                                      @RequestParam(value = "page", defaultValue = "1") Long page,
                                      @RequestParam(value = "pagesize", defaultValue = "10") Long pageSize,
                                      String nickname){

        PageResult<UserInfoVoLike> pageResult = userInfoService.findFriends(type, page, pageSize);

        return ResponseEntity.ok(pageResult);
    }
}
